﻿<template>
  <a-modal
    title="编辑策略任务"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [{required: true, message: '请输入标题！'}]}]" />
        </a-form-item>
        <a-form-item label="Cron表达式" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入Cron表达式" v-decorator="['crontabExpression']" />
        </a-form-item>
        <a-form-item label="摘要说明" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入摘要说明" v-decorator="['description']" />
        </a-form-item>
        <a-form-item  v-show="false" label="策略ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入策略ID" v-decorator="['policyId']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    PolicyTaskEdit
  } from '@/api/modular/main/PolicyTaskManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	// 初始化方法
	edit (record) {
	this.visible = true
	this.record = record
	setTimeout(() => {
	this.form.setFieldsValue(
	{
	id: record.id,
	title: record.title,
	crontabExpression: record.crontabExpression,
	description: record.description,
	policyId: record.policyId
	}
	)
	}, 100)
	},
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	this.record[key] = values[key]
	} else {
	this.record[key] = values[key]
	}
	}
	PolicyTaskEdit(this.record).then((res) => {
	if (res.success) {
	this.$message.success('编辑成功')
	this.confirmLoading = false
	this.$emit('ok', this.record)
	this.handleCancel()
	} else {
	this.$message.error('编辑失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
